import re
import requests


def get_movie_data(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
    }
    response = requests.get(url, headers=headers)

    if response.status_code == 200:
        response.encoding = 'utf-8'
        ret = response.text
    else:
        print('请求失败')
        return []

    pattern = '<li>.*?class="item">.*? <span class="title">(.*?)</span>.*?<span class="rating_num" property="v:average">(.*?)</span>.*?<span>(.*?)人评价</span>'
    match = re.findall(pattern, ret, re.S)
    return match


url = 'https://movie.douban.com/top250'
movie_data = get_movie_data(url)

for item in movie_data:
    movie_name = item[0]
    rating = item[1]
    evaluation = item[2]
    print(f'电影名字: {movie_name}\n评分: {rating}\n评价人数: {evaluation}\n')
